package action;

import java.io.File;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import validator.JqueryValidator;
import validator.Validators;

import com.opensymphony.xwork2.ActionSupport;

@Action
@Results({
	@Result(name="login",location="/login.jsp"),
	@Result(name="input",location="/login.jsp"),
//	@Result(name="input",location="/${resInput}.jsp"),
	@Result(location="/success.jsp")
})
/**
 * 登录验证示例
 * @author heantai
 */
@InterceptorRefs({
	@InterceptorRef(value="defaultStack"),
	@InterceptorRef(value="fileUpload")
})
public class DemoAction extends ActionSupport implements ServletRequestAware{
	
	private final static Logger log=LoggerFactory.getLogger(DemoAction.class);
	
	protected HttpServletRequest request;
	
	private String name;
	
	private String number;
	
	private Integer age;
	
	private String phone;
	
	private Integer age2;
	
	private String phone2;
	
	//由于struts上传文件得到的文件是.tmp文件，所以需要fileName字段来保存文件名以做判断
	//文件名的拦截应该在struts的文件上传拦截器中做
	private File file;
	private String fileName;
	
	
	/**验证未通过时返回到的页面*/
	private String resInput;
	
	
	private Date date;
	
	public void validateLogin(){
		Map<String, String> result=	JqueryValidator.validate(Validators.DEMO_ACTION_LOGIN_MAP,request);
		
		if(!result.isEmpty()){
			ServletActionContext.getRequest().setAttribute(JqueryValidator.ERR_MSGS, JqueryValidator.map2Json(result));
			setFieldErrors(JqueryValidator.initErrorMap());
			setResInput("login");//一个action中有多个表单验证时使用
		}
		
	}
	//TODO 注解login方法
	//@Validate(validator=Validators.DEMO_ACTION_LOGIN_MAP,input="login",inputField="errInput")
	//input，验证不通过返回的页面;inputField指定验证不通过时由此属性确定返回的页面,默认为resInput,@Result(name="input",location="/${errInput}.jsp"),
	/*
	 * validateLogin方法代码
		Map<String, String> result=	JqueryValidator.validate(validator);
		
		if(!result.isEmpty()){
			ServletActionContext.getRequest().setAttribute(JqueryValidator.ERR_MSGS, JqueryValidator.toJson(result));
			setFieldErrors(JqueryValidator.initErrorMap());
			
			setResInput(input);//resInput为一个父类的属性
			
		}
	 */
	public String login(){
		return SUCCESS;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


	public String getResInput() {
		return resInput;
	}

	public void setResInput(String resInput) {
		this.resInput = resInput;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Integer getAge2() {
		return age2;
	}
	public void setAge2(Integer age2) {
		this.age2 = age2;
	}
	public String getPhone2() {
		return phone2;
	}
	public void setPhone2(String phone2) {
		this.phone2 = phone2;
	}
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {

		this.request=request;
	}


}
